package cn.enjoy.mapstruct.controller;

import cn.enjoy.mapstruct.entity.User;
import cn.enjoy.mapstruct.entity.UserVO1;
import cn.enjoy.mapstruct.entity.UserVO2;
import cn.enjoy.mapstruct.mapconvert.UserCovert;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author K
 * @date 2021/7/27 下午4:54
 */
@RestController
public class MapstructController {

    @GetMapping("convert-entity")
    public Object convertEntity() {
        User user = User.builder()
                .id(1)
                .name("张三")
                .createTime("2020-04-01 11:05:07")
                .updateTime(LocalDateTime.now())
                .build();
        List<Object> objectList = new ArrayList<>();

        objectList.add(user);

        // 使用 mapstruct
        UserVO1 userVO1 = UserCovert.INSTANCE.toConvertVO1(user);
        objectList.add("user->userVO1:" + UserCovert.INSTANCE.toConvertVO1(user));
        objectList.add("userVO1->user:" + UserCovert.INSTANCE.fromConvertEntity1(userVO1));
        // 输出转换结果
        objectList.add("user->userVO2:" + UserCovert.INSTANCE.toConvertVO2(user));
        // 使用BeanUtils
        UserVO2 userVO22 = new UserVO2();
        BeanUtils.copyProperties(user, userVO22);
        objectList.add("user->userVO22:" + userVO22);
//        UserVO3 userVO3 = UserCovertBasic.INSTANCE.toConvertVO3(user);
//        objectList.add("user->userVO3:" + userVO3);
//        objectList.add("userVO3->user:" + UserCovertBasic.INSTANCE.fromConvertEntity3(userVO3));
        return objectList;
    }

    @GetMapping("convert-list")
    public Object convertList() {
        List<Object> objectList = new ArrayList<>();
        User user = User.builder()
                .id(1)
                .name("张三")
                .createTime("2020-04-01 11:05:07")
                .updateTime(LocalDateTime.now())
                .build();
        User user2 = User.builder()
                .id(1)
                .name("张三")
                .createTime("2020-04-01 11:05:07")
                .updateTime(LocalDateTime.now())
                .build();
        User user3 = User.builder()
                .id(1)
                .name("张三")
                .createTime("2020-04-01 11:05:07")
                .updateTime(LocalDateTime.now())
                .build();
        objectList.add(user);
        objectList.add(user2);
        objectList.add(user3);

        return UserCovert.INSTANCE.convertList(objectList, UserVO1.class);
    }

}